<template>
  <div style="padding: 10px">
<!--    功能区-->
    <div style="margin: 10px 0">
      <el-button type="primary" @click="add">新增</el-button>
      <el-button type="primary">导入</el-button>
    </div>
<!--    搜索区-->
    <div style="margin: 10px 0">
      <el-input v-model="search" placeholder="请输入关键字" style="width: 20%" clearable />
      <el-button type="primary" style="margin-left: 5px" @click="load">查询</el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%" stripe>
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="character" label="用户角色" />
      <el-table-column prop="personal_num" label="学号/工号" sortable/>
      <el-table-column prop="id" label="身份证" />
      <el-table-column prop="phone_number" label="手机号码" />
      <el-table-column prop="email" label="邮箱" />
      <el-table-column fixed="right" label="操作" width="120">
        <template #default="scope">
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm title="确定删除此信息？" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin: 10px 0">
      <el-pagination
          v-model:currentPage="currentPage"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />

      <el-dialog v-model="dialogVisible" title="提示" width="30%">
        <el-form :model="form" label-width="120px">
          <el-form-item label="姓名">
            <el-input v-model="form.name" style="width: 80%"/>
          </el-form-item>
          <el-form-item label="身份证">
            <el-input v-model="form.id" style="width: 80%"/>
          </el-form-item>
          <el-form-item label="用户角色">
            <el-radio v-model="form.character" label="1">学生</el-radio>
            <el-radio v-model="form.character" label="2">教师</el-radio>
          </el-form-item>
          <el-form-item label="学号/工号">
            <el-input v-model="form.personal_num" style="width: 80%"/>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="form.email" style="width: 80%"/>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="form.phone_number" style="width: 80%"/>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="save"
            >确 定</el-button
            >
          </span>
        </template>
      </el-dialog>

    </div>

  </div>
</template>

<script>

import request from "@/utils/request";

export default {
  name: 'HomeView',
  components: {
  },
  data() {
    return {
      form: {},
      dialogVisible: false,
      search: '',
      currentPage: 1,
      pageSize: 10,
      total: 2,

      tableData: [
        {
          name: '缅因',
          personal_num: '123321',
          character: '教师'
        }, {
          name: '团雀',
          personal_num: '12315',
          character: '学生'
        }

      ]
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      request.get("http://124.70.179.188:8082/api/user", {
        params:{
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.search
        }
      }).then(res => {
        console.log(res)
        //to do: 后台数据赋值渲染到前台
        this.tableData = res.data
        this.total = res.data.total
      })
    },
    add() {
      this.dialogVisible = true
      this.form = {}
    },
     //to do: 接口待完善！！！！
    save() {
       if (this.form.id) { //若id存在则更新数据（编辑），若id不存在则新增数据
        request.put("http://124.70.179.188:8082/api/user", this.form).then(res =>{
          console.log(res)
          // 判断是否返回成功，若失败给出失败的信息
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "更新成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load() // 刷新表格数据
          this.dialogVisible = false // 关闭弹窗
        })
      } else {
        request.post("http://124.70.179.188:8082/api/user", this.form).then(res => {
          console.log(res)
          // 判断是否返回成功，若失败给出失败的信息
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "新增成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load() // 刷新表格数据
          this.dialogVisible = false // 关闭弹窗
        })
      }
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
    },
    handleDelete(id) { // to do: 后台通过id进行删除
      console.log(id)
      request.delete("http://124.70.179.188:8082/api/user/" + id).then(res => {
        // 判断是否返回成功，若失败给出失败的信息
        if (res.code === '0') {
          this.$message({
            type: "success",
            message: "删除成功"
          })
        } else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
        this.load() // 删除后重新加载表格数据
      })
    },
    handleSizeChange(pageSize) { // 改变当前每页的个数触发
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) { //改变当前页码触发
      this.currentPage = pageNum
      this.load()
    }
  }
}
</script>
